package com.aos.eap.activity;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.CheckListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.CheckBean;
import com.aos.eap.bean.PeCheckBean;
import com.aos.eap.bean.RepairBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class CheckListActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.rvInputList)
    RecyclerView rvInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private RepairBean repairBean;
    private PeCheckBean peCheckBean;
    private CheckListAdapter checkListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_check_list;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        rvInputList.setLayoutManager(new LinearLayoutManager(CheckListActivity.this));
        rvInputList.setItemAnimator(new DefaultItemAnimator());
        rvInputList.addItemDecoration(new DividerItemDecoration(CheckListActivity.this, 1));

        checkListAdapter = new CheckListAdapter(null);
        checkListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkListAdapter.isFirstOnly(true);
        rvInputList.setAdapter(checkListAdapter);
        repairBean = (RepairBean) getIntent().getSerializableExtra("repairBean");
        requestInfo();
    }

    private void requestInfo() {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", repairBean.getEqpId());
        map.addProperty("RepairType", repairBean.getRepairType());
        map.addProperty("RepairSubtype", repairBean.getRepairSubType());
        NetUtils.requestGetNet(this, "EMS_RepairGetCheckList", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<PeCheckBean>>(
                new TypeToken<BaseResponseBean<PeCheckBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<PeCheckBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    peCheckBean = response.body().getResponse().getBody();
                    checkListAdapter.setNewData(peCheckBean.getCheckList());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PeCheckBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RepairGetCheckList");
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("检查");
        freeUI_titleView.setLeftListener(view -> showBackDialog(CheckListActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (peCheckBean == null) {
                showToast("未获取到页面信息，请重新拉取", false);
                return;
            }
            boolean isOk = true;
            for (CheckBean checkBean : checkListAdapter.getData()) {
                if (checkBean.getItemValue().length() == 0) {
                    isOk = false;
                    break;
                }
            }
            if (!isOk) {
                showToast("请填写所有检查项", false);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("UserID", Constant.CURRENT_USER);
            map.addProperty("EqpID", repairBean.getEqpId());
            map.addProperty("LotID", repairBean.getLotId());
            map.addProperty("BDID", repairBean.getBDId());
            map.addProperty("BDVer", repairBean.getBDVer());
            map.addProperty("RepairType", repairBean.getRepairType());
            map.addProperty("RepairSubType", repairBean.getRepairSubType());
            map.addProperty("RepairLevel", repairBean.getRepairLevel());
            map.addProperty("FaultDesc", repairBean.getFaultDesc());
            map.addProperty("IsBuyOff", repairBean.getIsBuyOff());
            map.addProperty("IsPECheck", repairBean.getISPECheck());
            map.addProperty("IsNewRcp", repairBean.getIsNewRcp());
            map.addProperty("MERepairType", repairBean.getMERepairType());
            map.addProperty("OpRemark", repairBean.getOpRemark());
            map.addProperty("DcType", repairBean.getDcType());
            map.add("CheckList", new JsonParser().parse(new Gson().toJson(checkListAdapter.getData())).getAsJsonArray());
            map.addProperty("Remark", repairBean.getRemark());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_RepairME", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(() -> {
                        FreeApi_ActivityManager.getActivityManager().finishActivity(EE_RepairActivity.class);
                        CheckListActivity.this.finish();
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_RepairME");
            }
        });
    }


    @OnClick(R.id.tvRequest)
    public void onViewClicked() {
        if (MyUtils.isFastClick()){
            showToast("您点击的太快了",false);
            return;
        }
        requestInfo();
    }
}
